package com.feilong.modules.controller.goods;

import com.feilong.modules.common.exception.APIException;
import com.feilong.modules.model.goods.constant.GoodsCategoryConstant;
import com.feilong.modules.model.goods.po.GoodsCategoryPO;
import com.feilong.modules.model.goods.request.AddCategoryRequest;
import com.feilong.modules.model.goods.request.UpdateCategoryRequest;
import com.feilong.modules.model.goods.vo.GoodsCategoryVO;
import com.feilong.modules.service.goods.GoodsCategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("goods/category")
public class GoodsCategoryController {

    @Resource
    GoodsCategoryService goodsCategoryService;

    /**
     * 获取所有商品分类
     * @return
     */
    @GetMapping("getCategory")
    public List<GoodsCategoryPO> getCategory(){
        return goodsCategoryService.getCategory();
    }

    /**
     * 根据商品分类id获取所有商品
     * @param categoryId
     * @return
     */
    @GetMapping("getGoods/{categoryId}")
    public List<GoodsCategoryVO> getCategoryDetail(@PathVariable("categoryId") Integer categoryId) {
        if (categoryId == null) {
            throw new APIException(GoodsCategoryConstant.CATEGORY_GOODS_UNEXIST);
        }
        return goodsCategoryService.getCategoryDetail(categoryId);
    }

    /**
     * 更新商品分类
     * @param request
     */
    @PostMapping("update")
    public void updateCategory(@RequestBody @Valid UpdateCategoryRequest request) {
        goodsCategoryService.updateCategory(request);
    }

    /**
     * 添加商品分类
     * @param request
     */
    @PostMapping("add")
    public void addCategory(@RequestBody @Valid AddCategoryRequest request) {
        goodsCategoryService.addCategory(request);
    }

    /**
     * 根据分类id删除商品分类信息
     * @param categoryId
     */
    @GetMapping("del")
    public void delCategory(@RequestParam("categoryId") Integer categoryId){
        if (categoryId < 1) {
            throw new APIException(GoodsCategoryConstant.DEL_CATEGORY_FAIL);
        }
        goodsCategoryService.delCategory(categoryId);
    }
}
